package edu.hubu.talentmanagementsystem.controller.impl;

import edu.hubu.talentmanagementsystem.common.CommonResult;
import edu.hubu.talentmanagementsystem.controller.AbsCrudController;
import edu.hubu.talentmanagementsystem.exception.InformationNotFoundException;
import edu.hubu.talentmanagementsystem.exception.InternalServerError;
import edu.hubu.talentmanagementsystem.model.ext.Staff;
import edu.hubu.talentmanagementsystem.service.IResourceService;
import edu.hubu.talentmanagementsystem.service.IStaffService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author moonlan
 * date 2021/3/2 下午9:50
 */
@SuppressWarnings(value = "unused")
@Api(tags = "staff相关接口")
@RestController
@RequestMapping("/staff")
public class StaffController extends AbsCrudController<Staff, Integer> {

    private final IStaffService service;

    private final IResourceService resourceService;

    public StaffController(IStaffService service,IResourceService resourceService) {
        this.service = service;
        this.resourceService = resourceService;
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataTypeClass = Integer.class)
    @Override
    public CommonResult selectOneById(@PathVariable Integer id, @RequestHeader("Authorization") String token) {
        Staff staff = service.selectById(id)
                .orElseThrow(() -> new InformationNotFoundException("Id=" + id + "的实体未找到"));
        return CommonResult.success(staff);
    }

    @GetMapping("/all")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("查询所有")
    @ApiImplicitParam(name = "order", value = "排序字段，可以为null或空", required = true, dataTypeClass = String.class)
    @Override
    public CommonResult selectAllOrderBy(@RequestHeader(name = "order", required = false)  String order, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.selectAll(order));
    }

    @GetMapping("/page-all")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("分页查询所有")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "currentPage", value = "当前页，默认为1", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，默认为5", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "order", value = "排序字段，可以为null或空", required = true, dataTypeClass = String.class)
    })
    @Override
    public CommonResult selectAllPagedOrderBy(
            @RequestParam(required = false, defaultValue = "1") Integer currentPage,
            @RequestParam(required = false, defaultValue = "5") Integer pageSize,
            @RequestHeader(name = "order", required = false)  String order, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.selectAllPaged(currentPage, pageSize, order));
    }

    @PostMapping("/like-page-all")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("分页模糊查询")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "currentPage", value = "当前页，默认为1", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，默认为5", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "order", value = "排序字段，可以为null或空", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "staff", value = "企业查询条件", required = true, dataTypeClass = Staff.class)
    })
    @Override
    public CommonResult selectLikePagedOrderBy(@RequestParam(required = false, defaultValue = "1") Integer currentPage,
            @RequestParam(required = false, defaultValue = "5") Integer pageSize, @RequestHeader(name = "order", required = false)  String order,
                                               @RequestBody Staff staff, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.selectLikePaged(currentPage, pageSize, staff, order));
    }

    @PostMapping("/")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("保存")
    @ApiImplicitParam(name = "staff", value = "企业查询条件", required = true, dataTypeClass = Staff.class)
    @Override
    public CommonResult save(@RequestBody Staff staff, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.insert(staff).orElseThrow(() -> new InternalServerError("保存失败")));
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("根据ID删除")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataTypeClass = Integer.class)
    @Override
    public CommonResult deleteById(@PathVariable Integer id, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.deleteById(id));
    }

    @PutMapping("/")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation("根据ID修改")
    @ApiImplicitParam(name = "staff", value = "企业修改条件", required = true, dataTypeClass = Staff.class)
    @Override
    public CommonResult updateById(@RequestBody Staff staff, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.updateById(staff.getStaffId(), staff)
                .orElseThrow(() -> new InternalServerError("修改失败")));
    }

    @PostMapping("/modifyPassword")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
//    @PreAuthorize("hasAnyAuthority('STAFF')")
    @ApiOperation("根据staffNum修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staffNum", value = "员工编号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "password", value = "新密码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header")
    })
    public CommonResult modifyPassword(@RequestParam String staffNum,@RequestParam String password, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.updateByStaffNum(staffNum,password)
                .orElseThrow(() -> new InternalServerError("修改失败")));
    }

    @GetMapping("/staffInfo")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
//    @PreAuthorize("hasAnyAuthority('STAFF')")
    @ApiOperation("根据员工id查看个人信息(包括头像和身份)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staffId", value = "员工编号", required = true, paramType = "query"),
    })
    public CommonResult staffInfo(@RequestParam Integer staffId, @RequestHeader("Authorization") String token) {
        return CommonResult.success(service.findStaffInfoById(staffId)
                .orElseThrow(() -> new InternalServerError("查询失败")));
    }


    @PatchMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    public CommonResult updatePwd(@PathVariable Integer id, String curPwd, @RequestHeader("Authorization") String updatePwd) {
        return CommonResult.success(service.updatePwd(id, curPwd, updatePwd));
    }

    @PostMapping("/register")
    @ApiOperation("员工注册")
    @ApiImplicitParam(name = "staff", value = "员工实体类", required = true, dataTypeClass = Staff.class)
    public CommonResult register(@RequestBody Staff staff) {
        staff.setStaffImgResourceId(resourceService.selectDefaultHead("staff").getResourceId());
        return CommonResult.success(service.register(staff));
    }

    @GetMapping("/getCompanyStaff")
    @ApiOperation("公司获取员工列表")
    @PreAuthorize("hasAnyAuthority('ADMIN','COMPANY','STAFF')")
    @ApiImplicitParam(name = "companyId", value = "公司id", required = true, paramType = "query")
    public CommonResult getCompanyStaff(@RequestParam Integer companyId) {
        return CommonResult.success(service.selectListByCompanyId(companyId));
    }
}
